package com.ruoyi.testing.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report.service.IBgbhService;
import com.ruoyi.testing.domain.EmploymentNotice;
import com.ruoyi.testing.domain.EmploymentPeople;
import com.ruoyi.testing.mapper.EmploymentNoticeMapper;
import com.ruoyi.testing.mapper.EmploymentPeopleMapper;
import com.ruoyi.testing.service.IEmploymentNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 从业告知书Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-17
 */
@Service
public class EmploymentNoticeServiceImpl implements IEmploymentNoticeService
{
    @Autowired(required = false)
    private EmploymentNoticeMapper employmentNoticeMapper;

    @Autowired(required = false)
    private EmploymentPeopleMapper employmentPeopleMapper;

    @Autowired(required = false)
    private IBgbhService bgbhService;

    /**
     * 查询从业告知书
     * 
     * @param gzsId 从业告知书主键
     * @return 从业告知书
     */
    @Override
    public EmploymentNotice selectEmploymentNoticeByGzsId(Long gzsId)
    {
        EmploymentNotice employmentNotice = employmentNoticeMapper.selectEmploymentNoticeByGzsId(gzsId);
        List<EmploymentPeople> employmentPeopleList = employmentPeopleMapper.selectEmploymentPeopleByGzsId(gzsId);
        employmentPeopleList.forEach(item -> {
            item.setRyIds(item.getRyId().split(","));
            if(StringUtils.isNotEmpty(item.getXmid())) {
                if(StringUtils.isNotEmpty(employmentNotice.getXmhy())) {
                    //获取关联的设备类型
                    item.setEquType(DictUtils.getDictRemark(employmentNotice.getXmhy().toLowerCase(), item.getXmid(), ""));
                }
            }
        });
        employmentPeopleList = employmentPeopleList.stream().filter(item -> StringUtils.isNotEmpty(item.getEquType())).collect(Collectors.toList());
        employmentNotice.setList(employmentPeopleList);
        return employmentNotice;
    }

    /**
     * 任务通知书查询从业告知书
     *
     * @param gzsId 从业告知书主键
     * @return 从业告知书
     */
    @Override
    public EmploymentNotice selectEntrustmentByGzsId(Long gzsId) {
        EmploymentNotice employmentNotice = employmentNoticeMapper.selectEmploymentNoticeByGzsId(gzsId);
        List<EmploymentPeople> employmentPeopleList = employmentPeopleMapper.selectEmploymentPeopleByGzsId(gzsId);
        employmentPeopleList.forEach(item -> {
            item.setRyIds(item.getRyId().split(","));
            if(StringUtils.isNotEmpty(item.getXmid())) {
                if(StringUtils.isNotEmpty(employmentNotice.getXmhy())) {
                    //获取关联的设备类型
                    item.setEquType(DictUtils.getDictRemark(employmentNotice.getXmhy().toLowerCase(), item.getXmid(), ""));
                }
                //当前设备类型最大编号
                item.setBgbh(bgbhService.selectBgbhMax(item.getXmid()));
            }
        });
        employmentPeopleList = employmentPeopleList.stream().filter(item -> StringUtils.isNotEmpty(item.getEquType())).collect(Collectors.toList());
        employmentNotice.setList(employmentPeopleList);
        return employmentNotice;
    }

    /**
     * 查询从业告知书列表
     * 
     * @param employmentNotice 从业告知书
     * @return 从业告知书
     */
    @Override
    public List<EmploymentNotice> selectEmploymentNoticeList(EmploymentNotice employmentNotice)
    {
        return employmentNoticeMapper.selectEmploymentNoticeList(employmentNotice);
    }

    /**
     * 查询包含委托单位和检测日期的告知书列表信息
     */
    @Override
    public List<Map<String, Object>> selectClientAndTestTimeNotice() {
        EmploymentNotice employmentNotice = new EmploymentNotice();
        //查询所有告知书信息
        List<EmploymentNotice> employmentNoticeList = employmentNoticeMapper.selectEmploymentNoticeList(employmentNotice);
        //返回的拼接数据信息
        List<Map<String, Object>> noticeList = new ArrayList<>();
        if(!employmentNoticeList.isEmpty()) {
            employmentNoticeList.forEach(item -> {
                Map<String, Object> noticeMap = new HashMap<>();
                noticeMap.put("id", item.getGzsId());
                noticeMap.put("name", item.getWtdwMc() + "(" + DateUtils.formatDate(item.getJckshsj(), DateUtils.YYYY_MM_DD) + "-" + DateUtils.formatDate(item.getJcjssj(), DateUtils.YYYY_MM_DD) + ")");
                noticeList.add(noticeMap);
            });
        }
        return noticeList;
    }

    /**
     * 新增从业告知书
     * 
     * @param employmentNotice 从业告知书
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertEmploymentNotice(EmploymentNotice employmentNotice)
    {
        //添加人员信息
        int ret = employmentNoticeMapper.insertEmploymentNotice(employmentNotice);
        //新增明细信息
        if(ret > 0 && employmentNotice.getList().size() > 0) {
            employmentNotice.getList().forEach(item -> {
                item.setGzsId(employmentNotice.getGzsId());
                item.setRyId(StringUtils.join(item.getRyIds(), ","));
            });
            employmentPeopleMapper.insertEmploymentPeoples(employmentNotice.getList());
        }
        return employmentNotice.getGzsId();
    }

    /**
     * 修改从业告知书
     * 
     * @param employmentNotice 从业告知书
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long updateEmploymentNotice(EmploymentNotice employmentNotice)
    {
        //更新协议
        int ret = employmentNoticeMapper.updateEmploymentNotice(employmentNotice);
        if(ret > 0 && employmentNotice.getList().size() > 0) {
            //删除原有数据
            int dRet = employmentPeopleMapper.deleteEmploymentPeopleByGzsId(employmentNotice.getGzsId());
            //新增数据
            if (dRet >= 0 && employmentNotice.getList().size() > 0) {
                employmentNotice.getList().forEach(item -> {
                    item.setGzsId(employmentNotice.getGzsId());
                    item.setRyId(StringUtils.join(item.getRyIds(), ","));
                });
                employmentPeopleMapper.insertEmploymentPeoples(employmentNotice.getList());
            }
        }
        return employmentNotice.getGzsId();
    }

    /**
     * 批量删除从业告知书
     * 
     * @param gzsIds 需要删除的从业告知书主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEmploymentNoticeByGzsIds(Long[] gzsIds)
    {
        for(int i = 0; i < gzsIds.length; i++) {
            employmentPeopleMapper.deleteEmploymentPeopleByGzsId(gzsIds[i]);
        }
        return employmentNoticeMapper.deleteEmploymentNoticeByGzsIds(gzsIds);
    }

    /**
     * 删除从业告知书信息
     * 
     * @param gzsId 从业告知书主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEmploymentNoticeByGzsId(Long gzsId)
    {
        employmentPeopleMapper.deleteEmploymentPeopleByGzsId(gzsId);
        return employmentNoticeMapper.deleteEmploymentNoticeByGzsId(gzsId);
    }
}
